﻿using Ewell.BizComponent.UI.Person.ViewModel;
using Ewell.Interface.Common.Model.GateWay.Dto.Basic;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.UI.Person.Views
{
    /// <summary>
    /// CreatePersonView.xaml 的交互逻辑
    /// </summary>
    public partial class CreatePersonView : UserControl
    {
        public IDialogCoordinator dialogCoordinator { get; set; } = DialogCoordinator.Instance;
        public static readonly DependencyProperty CreatePersonVmProperty = DependencyProperty
            .Register("createPersonVm", typeof(CreatePersonVm), typeof(CreatePersonView),
            new FrameworkPropertyMetadata(new CreatePersonVm()));
        public CreatePersonVm createPersonVm
        {
            get
            {
                if (this.DataContext?.GetType() != typeof(CreatePersonVm))
                    return (CreatePersonVm)GetValue(CreatePersonVmProperty);
                else
                    return this.DataContext as CreatePersonVm;
            }
            set { SetValue(CreatePersonVmProperty, value); }
        }
        public CreatePersonView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            #region 控件分类
            List<string> ownerFields = new List<string>() {
                "patientId",
                "fixedPhone",
                "birthplace",
                "supremeEducationCoding",
                "lemologyCategoryCoding",
                "patEnName",
                "name",
                "patientTypeNo",
                "sex",
                "birthday",
                "nativePlace",
                "nation",
                "country",
                "tel",
                "telB",
                "marriage",
                "occupationNo",
                "workPlace",
                "email",
                "familyAddress",
                "zipCode",
                "accountAddress",
                "accountZipCode",
                "workPlaceTel",
                "workPlaceAddress",
                "workPlaceZipCode",
            };
            List<string> contactFields = new List<string>()
            {
                "linkManIdNumber",
                "linkMan",
                "linkManCardType",
                "linkManRelation",
                "linkManRelation",
                "linkManTel",
                "linkManTelB",
                "linkManAddress",
                "linkManAddress",
                "remarks",
            };
            #endregion

            //查询建档配置数据
            MemberConfigSelectInitRequest memberConfigSelectInitRequest = new MemberConfigSelectInitRequest()
            {
                type = "3",
            };
            ConfigMemberDubboService configMemberDubboService = new ConfigMemberDubboService();
            var cfgGateWayResult=configMemberDubboService.memberConfigSelectInit.Post<List<PersonControlVm>>(memberConfigSelectInitRequest);
            if(!cfgGateWayResult.success)
            {
                Dispatcher.BeginInvoke(async () =>
                {
                    await this.dialogCoordinator.ShowMessageAsync(this, "警告", $"初始化界面失败!查询建档配置数据出错!{cfgGateWayResult.message}");
                });
            }
            createPersonVm.personControlList = cfgGateWayResult.@object;
            createPersonVm.ownerControlList= createPersonVm.personControlList.Where(w=> ownerFields.Contains(w.field)).ToList();
            createPersonVm.contactControlList = createPersonVm.personControlList.Where(w => contactFields.Contains(w.field)).ToList();
            this.DataContext = createPersonVm;
        }
    }
}
